package com.winit.openapi.fsp.utils;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.spring.SpringMessageLocator;
import com.winit.mms.spi.goods.command.ImportFspGoodsCommand;
import com.winit.mms.spi.goods.vo.ImportFspGoodsErrMsg;

/**
 * 金额商品字段校验工具类 <Change to the actual description of this class>
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * BW 		1.0  		2015-8-26 	Created
 * 
 * </pre>
 * @since 1.
 */
public class FspGoodsFieldValidator {

    /**
     * 校验金融商品字段
     * 
     * @param json
     * @author BW
     * @date 2015-8-27
     */
    public static int validFspGoodsField(List<ImportFspGoodsErrMsg> listErrMsg, JSONObject json,
                                         ImportFspGoodsCommand command) {
        String lineNo = "";
        if (json.containsKey("lineNo")) {
            lineNo = json.getString("lineNo");
        }
        /** 商品编码 */
        validNotNull(listErrMsg,json,"goodsCode",SpringMessageLocator.getMessage(GoodsConstant.GOODS_CODE_IS_NULL, 
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg,json,"goodsCode",SpringMessageLocator.getMessage(GoodsConstant.GOODS_CODE_LENGTH_ERROR,
            new String[] { "50" },
            command.getCtx().getLocale()),20);
        /** 规格 */
        if(null !=json.getString("specification")&& !"".equals(json.getString("specification"))){
            validMaxLength(listErrMsg, json, "specification", SpringMessageLocator.getMessage(GoodsConstant.SPECIFICATION_LENGTH_ERROR,
                new String[] { "40" },
                command.getCtx().getLocale()), 40);
        }
        /** 中文名称 */
        validNotNull(listErrMsg, json, "cnName", SpringMessageLocator.getMessage(GoodsConstant.CN_NAME_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "cnName", SpringMessageLocator.getMessage(GoodsConstant.CN_NAME_LENGTH_ERROR,
            new String[] { "125" },
            command.getCtx().getLocale()), 125);
        /** 英文名称 */
        validNotNull(listErrMsg, json, "enName", SpringMessageLocator.getMessage(GoodsConstant.EN_NAME_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "enName", SpringMessageLocator.getMessage(GoodsConstant.EN_NAME_LENGTH_ERROR,
            new String[] { "125" },
            command.getCtx().getLocale()), 125);
         
        /** 卖家一级分类ID */
        validNotNull(listErrMsg, json, "sellerClassifyId1", SpringMessageLocator.getMessage(GoodsConstant.SELLER_CLASSIFY_IS_NOT_NULL,
            command.getCtx().getLocale()));
        /** 用途 */
        validNotNull(listErrMsg, json, "goodsUse", SpringMessageLocator.getMessage(GoodsConstant.GOODS_USE_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "goodsUse", SpringMessageLocator.getMessage(GoodsConstant.GOODS_USE_LENGTH_ERROR,
            new String[] { "40" },
            command.getCtx().getLocale()), 40);
        /** 单位 */
        validNotNull(listErrMsg, json, "unit", SpringMessageLocator.getMessage(GoodsConstant.UNIT_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "unit", SpringMessageLocator.getMessage(GoodsConstant.UNIT_LENGTH_ERROR,
            new String[] { "10" },
            command.getCtx().getLocale()), 10);
        /** 型号 */
        validNotNull(listErrMsg, json, "model", SpringMessageLocator.getMessage(GoodsConstant.MODEL_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "model", SpringMessageLocator.getMessage(GoodsConstant.MODEL_LENGTH_ERROR,
            new String[] { "20" },
            command.getCtx().getLocale()), 20);
        /** 材质 */
        validNotNull(listErrMsg, json, "material", SpringMessageLocator.getMessage(GoodsConstant.MATERIAL_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "material", SpringMessageLocator.getMessage(GoodsConstant.MATERIAL_LENGTH_ERROR,
            new String[] { "40" },
            command.getCtx().getLocale()), 40);
        /** 颜色 */
        validNotNull(listErrMsg, json, "color", SpringMessageLocator.getMessage(GoodsConstant.COLOR_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "color", SpringMessageLocator.getMessage(GoodsConstant.COLOR_LENGTH_ERROR,
            new String[] { "40" },
            command.getCtx().getLocale()), 40);
        /** 重量 */
        validNotNull(listErrMsg, json, "weight",SpringMessageLocator.getMessage(GoodsConstant.WEIGHT_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validBigDecimal(listErrMsg, json, "weight", SpringMessageLocator.getMessage(GoodsConstant.WEIGHT_IS_NOT_NUMBER,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "weight", SpringMessageLocator.getMessage(GoodsConstant.WEIGHT_LENGTH_ERROR,
            new String[] { "20" },
            command.getCtx().getLocale()), 20); 
        numberTwo(listErrMsg, json,"weight", SpringMessageLocator.getMessage(GoodsConstant.WEIGHT_PRECISION_ERROR,
            new String[] { "2" },
            command.getCtx().getLocale()), 2, lineNo);
        /** 长 */
        validNotNull(listErrMsg, json, "goodsLength", SpringMessageLocator.getMessage(GoodsConstant.GOODS_LENGTH_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validBigDecimal(listErrMsg, json, "goodsLength", SpringMessageLocator.getMessage(GoodsConstant.GOODS_LENGTH_IS_NOT_NUMBER,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "goodsLength", SpringMessageLocator.getMessage(GoodsConstant.GOODS_LENGTH_LENGTH_ERROR,
            new String[] { "20" },
            command.getCtx().getLocale()), 20);
        numberTwo(listErrMsg, json,"goodsLength",SpringMessageLocator.getMessage(GoodsConstant.GOODS_LENGTH_PRECISION_ERROR,
            new String[] { "2" },
            command.getCtx().getLocale()), 2, lineNo);
        /** 宽 */
        validNotNull(listErrMsg, json, "width", SpringMessageLocator.getMessage(GoodsConstant.WIDTH_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validBigDecimal(listErrMsg, json, "width", SpringMessageLocator.getMessage(GoodsConstant.WIDTH_IS_NOT_NUMBER,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "width", SpringMessageLocator.getMessage(GoodsConstant.WIDTH_LENGTH_ERROR,
            new String[] { "20" },
            command.getCtx().getLocale()), 20);
        numberTwo(listErrMsg, json,"width", SpringMessageLocator.getMessage(GoodsConstant.WIDTH_PRECISION_ERROR,
            new String[] { "2" },
            command.getCtx().getLocale()), 2, lineNo);
        /** 高 */
        validNotNull(listErrMsg, json, "high", SpringMessageLocator.getMessage(GoodsConstant.HIGH_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validBigDecimal(listErrMsg, json, "high", SpringMessageLocator.getMessage(GoodsConstant.HIGH_IS_NOT_NUMBER,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "high", SpringMessageLocator.getMessage(GoodsConstant.HIGH_LENGTH_ERROR,
            new String[] { "20" },
            command.getCtx().getLocale()), 20);
        numberTwo(listErrMsg, json,"high", SpringMessageLocator.getMessage(GoodsConstant.HIGH_PRECISION_ERROR,
            new String[] { "2" },
            command.getCtx().getLocale()), 2, lineNo);
        /** 品牌分类 */
        validNotNull(listErrMsg, json, "brandCategoryId", SpringMessageLocator.getMessage(GoodsConstant.BRAND_CATEGORY_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "brandCategoryId", SpringMessageLocator.getMessage(GoodsConstant.BRAND_CATEGORY_LENGTH_ERROR,
            new String[] { "100" },
            command.getCtx().getLocale()), 100);
        if (!json.getString("brandCategoryId").equals("other")) {
            /** 品牌名称 */
            validNotNull(listErrMsg, json, "brandName", SpringMessageLocator.getMessage(GoodsConstant.BRANDNAME_IS_NOT_NULL,
                command.getCtx().getLocale()));
            validMaxLength(listErrMsg, json, "brandName", SpringMessageLocator.getMessage(GoodsConstant.BRANDNAME_LENGTH_ERROR,
                new String[] { "30" },
                command.getCtx().getLocale()), 30);
        }else{
            json.put("brandName", "");
        }
        
        /** 是否含电池 */
        validNotNull(listErrMsg, json, "isBattery", SpringMessageLocator.getMessage(GoodsConstant.IS_BATTERY_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "isBattery", SpringMessageLocator.getMessage(GoodsConstant.IS_BATTERY_LENGTH_ERROR,
            new String[] { "1" },
            command.getCtx().getLocale()), 1);
        /** 网址 */
        validNotNull(listErrMsg, json, "internetSite", SpringMessageLocator.getMessage(GoodsConstant.INTERNETSITE_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "internetSite", SpringMessageLocator.getMessage(GoodsConstant.INTERNETSITE_LENGTH_ERROR,
            new String[] { "200" },
            command.getCtx().getLocale()), 200);
        /** 电商海外月均销量 */
        if(null !=json.getString("overseasSales")&&!"".equals(json.getString("overseasSales"))){
            validMaxLength(listErrMsg, json, "overseasSales", SpringMessageLocator.getMessage(GoodsConstant.OVERSEAS_SALES_LENGTH_ERROR,
                new String[] { "16" },
                command.getCtx().getLocale()), 16);
            validBigDecimal(listErrMsg, json, "overseasSales", SpringMessageLocator.getMessage(GoodsConstant.OVERSEAS_SALES_IS_NOT_NUMBER,
                command.getCtx().getLocale()));
            numberTwo(listErrMsg, json,"overseasSales", SpringMessageLocator.getMessage(GoodsConstant.OVERSEAS_SALES_PRECISION_ERROR,
                new String[] { "2" },
                command.getCtx().getLocale()), 2, lineNo);
        }
        /** 出口HS编码 */
        validNotNull(listErrMsg, json, "hsCode", SpringMessageLocator.getMessage(GoodsConstant.HS_CODE_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "hsCode", SpringMessageLocator.getMessage(GoodsConstant.HS_CODE_LENGTH_ERROR,
            new String[] { "16" },
            command.getCtx().getLocale()), 16);
        /** 出口口岸 */
        validNotNull(listErrMsg, json, "exportSeaport", SpringMessageLocator.getMessage(GoodsConstant.EXPORT_SEAPORT_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "exportSeaport", SpringMessageLocator.getMessage(GoodsConstant.EXPORT_SEAPORT_LENGTH_ERROR,
            new String[] { "10" },
            command.getCtx().getLocale()), 10);
        /** 出口报价（RMB） */
        validNotNull(listErrMsg, json, "exportPricesRmb", SpringMessageLocator.getMessage(GoodsConstant.EXPORT_PRICES_RMB_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validBigDecimal(listErrMsg, json, "exportPricesRmb", SpringMessageLocator.getMessage(GoodsConstant.EXPORT_PRICES_RMB_IS_NOT_NUMBER,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "exportPricesRmb", SpringMessageLocator.getMessage(GoodsConstant.EXPORT_PRICES_RMB_LENGTH_ERROR,
            new String[] { "10" },
            command.getCtx().getLocale()), 10);
        numberTwo(listErrMsg,json, "exportPricesRmb", SpringMessageLocator.getMessage(GoodsConstant.EXPORT_PRICES_RMB_PRECISION_ERROR,
            new String[] { "2" },
            command.getCtx().getLocale()), 2, lineNo);
        /** 出口报价（USD） */
        validNotNull(listErrMsg, json, "exportPricesUsd", SpringMessageLocator.getMessage(GoodsConstant.EXPORT_PRICES_USD_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validBigDecimal(listErrMsg, json, "exportPricesUsd", SpringMessageLocator.getMessage(GoodsConstant.EXPORT_PRICES_USD_IS_NOT_NUMBER,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "exportPricesUsd", SpringMessageLocator.getMessage(GoodsConstant.EXPORT_PRICES_USD_PRECISION_ERROR,
            new String[] { "10" },
            command.getCtx().getLocale()), 10);
        numberTwo(listErrMsg,json, "exportPricesUsd", SpringMessageLocator.getMessage(GoodsConstant.EXPORT_PRICES_USD_PRECISION_ERROR,
            new String[] { "2" },
            command.getCtx().getLocale()), 2, lineNo);
        /** 专利证书号 */
        if(null !=json.getString("patentCertificateNumber")&& !"".equals(json.getString("patentCertificateNumber"))){
            validMaxLength(listErrMsg, json, "patentCertificateNumber", SpringMessageLocator.getMessage(GoodsConstant.PATENT_CERTIFICATE_NUMBER_LENGTH_ERROR,
                new String[] { "30" },
                command.getCtx().getLocale()), 30);
        }
        /** 是否成套售卖 */
        validNotNull(listErrMsg, json, "isWholeSale",  SpringMessageLocator.getMessage(GoodsConstant.IS_WHOLESALE_IS_NOT_NULL,
            command.getCtx().getLocale()));
        validMaxLength(listErrMsg, json, "isWholeSale", SpringMessageLocator.getMessage(GoodsConstant.IS_WHOLESALE_LENGTH_ERROR,
            new String[] { "1" },
            command.getCtx().getLocale()), 1);
        if (json.getString("isWholeSale").equals("Y")) {
            /** 单套产品数量 */
            validNotNull(listErrMsg, json, "wholeSaleDesc", SpringMessageLocator.getMessage(GoodsConstant.WHOLESALEDESC_IS_NOT_NULL,
                command.getCtx().getLocale()));
            validMaxLength(listErrMsg, json, "wholeSaleDesc", SpringMessageLocator.getMessage(GoodsConstant.WHOLESALEDESC_LENGTH_ERROR,
                new String[] { "100" },
                command.getCtx().getLocale()), 100);
        } else {
            if(null !=json.getString("wholeSaleDesc") && !"".equals(json.getString("wholeSaleDesc"))){
                validMaxLength(listErrMsg, json, "wholeSaleDesc", SpringMessageLocator.getMessage(GoodsConstant.WHOLESALEDESC_LENGTH_ERROR,
                    new String[] { "100" },
                    command.getCtx().getLocale()), 100);
            }
        }
        /****************** certificateList 证书列表 *******************/
            JSONArray crList = json.getJSONArray("certificateList");
            if(null !=crList){
                for (Object object : crList) {
                    JSONObject cerJson = (JSONObject) object;
                    cerJson.put("lineNo", json.get("lineNo"));
                    /** 证书类型编码 */
                    if (cerJson.containsKey("certificateTypeCode")) {
                        validMaxLength(listErrMsg, cerJson, "certificateTypeCode", SpringMessageLocator.getMessage(GoodsConstant.CERTIFICATE_TYPE_CODE_LENGTH_ERROR,
                            new String[] { "30" },
                            command.getCtx().getLocale()), 30);
                    }
                    /** 证书编号 */
                    if (cerJson.containsKey("certificateNo")) {
                        validMaxLength(listErrMsg, cerJson, "certificateNo", SpringMessageLocator.getMessage(GoodsConstant.CERTIFICATENO_LENGTH_ERROR,
                            new String[] { "30" },
                            command.getCtx().getLocale()), 30);
                    }
                }
            }

        /****************** customsList 海关信息 *******************/
         
            JSONArray cusList = json.getJSONArray("customsList");
            if (null == cusList) {
                if(listErrMsg.size()<GoodsConstant.ERROR_COUNT){
                    ImportFspGoodsErrMsg msg = new ImportFspGoodsErrMsg(lineNo,  SpringMessageLocator.getMessage(GoodsConstant.CUSTOMS_LIST_IS_NOT_NULL,
                        command.getCtx().getLocale()));
                    listErrMsg.add(msg);
                }
            } else {
                for (Object object : cusList) {
                    JSONObject cerJson = (JSONObject) object;
                    cerJson.put("lineNo", json.get("lineNo"));
                    /** 国家编码 */
                    if (cerJson.containsKey("countryCode")) {
                        validMaxLength(listErrMsg, cerJson, "countryCode", SpringMessageLocator.getMessage(GoodsConstant.COUNTRY_CODE_LENGTH_ERROR,
                            new String[] { "10" },
                            command.getCtx().getLocale()), 10);
                        if(cerJson.getString("countryCode")!=null&&!"".equals(cerJson.getString("countryCode"))){
                            /** 出口申报价值 */
                            if (cerJson.containsKey("value")) {
                                validNotNull(listErrMsg, cerJson, "value", SpringMessageLocator.getMessage(GoodsConstant.VALUE_IS_NOT_NULL,
                                    command.getCtx().getLocale()));
                                validMaxLength(listErrMsg, cerJson, "value", SpringMessageLocator.getMessage(GoodsConstant.VALUE_LENGTH_ERROR,
                                    new String[] { "10" },
                                    command.getCtx().getLocale()), 10);
                                validBigDecimal(listErrMsg, cerJson, "value", SpringMessageLocator.getMessage(GoodsConstant.VALUE_IS_NOT_NUMBER,
                                    command.getCtx().getLocale()));
                                if(cerJson.getString("value")!=null && !"".equals(cerJson.getString("value"))){
                                    numberTwo(listErrMsg, cerJson,"value", SpringMessageLocator.getMessage(GoodsConstant.VALUE_PRECISION_ERROR,
                                        new String[] { "2" },
                                        command.getCtx().getLocale()), 2, lineNo);
                                }
                            }
                        }
                    } 
                    
                }
            }

        return listErrMsg.size();
    }

    /**
     * 校验BigDecimal精确位数
     * 
     * @param big
     * @param key
     * @param value
     */
    public static void numberTwo(List<ImportFspGoodsErrMsg> listErrMsg, JSONObject json,String key, String aliasKey, int value,
                                 String lineNo) {
        if (listErrMsg.size() < GoodsConstant.ERROR_COUNT) {
                BigDecimal val=BigDecimal.ZERO;
            try {
                val=json.getBigDecimal(key);
                if (val.scale() > value) {
                    ImportFspGoodsErrMsg msg = new ImportFspGoodsErrMsg(lineNo, aliasKey);
                    listErrMsg.add(msg);
                }
            } catch (NumberFormatException e) {
                ImportFspGoodsErrMsg msg = new ImportFspGoodsErrMsg(lineNo, aliasKey);
                listErrMsg.add(msg);
            }catch(Exception e){
                ImportFspGoodsErrMsg msg = new ImportFspGoodsErrMsg(lineNo, aliasKey);
                listErrMsg.add(msg);
            } 
            
        }
    }

    public static void validNotNull(List<ImportFspGoodsErrMsg> listErrMsg, JSONObject json, String key, String aliasKey) {
        if (listErrMsg.size() < GoodsConstant.ERROR_COUNT) {
            if (json.containsKey(key)) {
                Object obj = json.get(key);
                if (obj != null && StringUtils.isNotBlank(obj.toString())) return;
            }
            if (StringUtils.isNotBlank(aliasKey)) {
                key = aliasKey;
            }
            ImportFspGoodsErrMsg msg = new ImportFspGoodsErrMsg(json.getString("lineNo"), aliasKey);
            listErrMsg.add(msg);
        }

    }

    public static void validMaxLength(List<ImportFspGoodsErrMsg> listErrMsg, JSONObject json, String key,
                                      String aliasKey, int maxLength, boolean... required) {
        if (listErrMsg.size() < GoodsConstant.ERROR_COUNT) {
            if (json.containsKey(key)) {
                Object obj = json.get(key);
                if (StringUtils.isNotBlank(aliasKey)) {
                    key = aliasKey;
                }
                if (obj != null && StringUtils.isNotBlank(obj.toString())) {
                    String s = obj.toString();
                    if (s.length() > maxLength) {
                        ImportFspGoodsErrMsg msg = new ImportFspGoodsErrMsg(json.getString("lineNo"), aliasKey);
                        listErrMsg.add(msg);

                    }
                } else {
                    if (required.length > 0 && required[0]) {
                        ImportFspGoodsErrMsg msg = new ImportFspGoodsErrMsg(json.getString("lineNo"), aliasKey);
                        listErrMsg.add(msg);
                    }
                }
            }
        }
    }
    
    
    public static void validBigDecimal(List<ImportFspGoodsErrMsg> listErrMsg,JSONObject json,String key,String aliasKey,boolean...required){
        if (listErrMsg.size() < GoodsConstant.ERROR_COUNT) {
            if(json.containsKey(key)){
                Object obj = json.get(key);
                if(obj != null && StringUtils.isNotBlank(obj.toString())){
                    String s = obj.toString();
                    try {
                        new BigDecimal(s);
                    } catch (NumberFormatException e) {
                        ImportFspGoodsErrMsg msg = new ImportFspGoodsErrMsg(json.getString("lineNo"), aliasKey);
                        listErrMsg.add(msg);
                    } catch(Exception e){
                        ImportFspGoodsErrMsg msg = new ImportFspGoodsErrMsg(json.getString("lineNo"), aliasKey);
                        listErrMsg.add(msg);
                    }
                }else{
                    if(required.length > 0 && required[0]){
                        ImportFspGoodsErrMsg msg = new ImportFspGoodsErrMsg(json.getString("lineNo"), aliasKey);
                        listErrMsg.add(msg);
                    }
                }
            }
        }
    }
}
